home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / PrintWriter.java < prev    next >
Text File  |  1998-09-22  |  9KB  |  379 lines

  1. /*
  2.  * @(#)PrintWriter.java    1.13 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17.  
  18. /**
  19.  * Print formatted representations of objects to a text-output stream.  This
  20.  * class implements all of the print methods found in PrintStream.  It does not
  21.  * contain methods for writing raw bytes, for which a program should use
  22.  * unencoded byte streams.
  23.  *
  24.  * <p> Unlike the PrintStream class, if automatic flushing is enabled it will
  25.  * be done only when one of the println() methods is invoked, rather than
  26.  * whenever a newline character happens to be output.  The println() methods
  27.  * use the platform's own notion of line separator rather than the newline
  28.  * character.
  29.  *
  30.  * <p> Methods in this class never throw I/O exceptions.  The client may
  31.  * inquire as to whether any errors have occurred by invoking checkError().
  32.  *
  33.  * @version     1.11, 97/01/27
  34.  * @author    Frank Yellin
  35.  * @author    Mark Reinhold
  36.  * @since    JDK1.1
  37.  */
  38.  
  39. public class PrintWriter extends Writer {
  40.  
  41.     private Writer out;
  42.     private boolean autoFlush = false;
  43.     private boolean trouble = false;
  44.  
  45.     /**
  46.      * Line separator string.  This is the value of the line.separator
  47.      * property at the moment that the stream was created.
  48.      */
  49.     private String lineSeparator;
  50.  
  51.     /**
  52.      * Create a new PrintWriter, without automatic line flushing.
  53.      *
  54.      * @param  out        A character-output stream
  55.      */
  56.     public PrintWriter (Writer out) {
  57.     this(out, false);
  58.     }
  59.  
  60.     /**
  61.      * Create a new PrintWriter.
  62.      *
  63.      * @param  out        A character-output stream
  64.      * @param  autoFlush  A boolean; if true, the println() methods will flush
  65.      *                    the output buffer
  66.      */
  67.     public PrintWriter(Writer out,
  68.                boolean autoFlush) {
  69.     super(out);
  70.     this.out = out;
  71.     this.autoFlush = autoFlush;
  72.     lineSeparator = System.getProperty("line.separator");
  73.     }
  74.  
  75.     /**
  76.      * Create a new PrintWriter, without automatic line flushing, from an
  77.      * existing OutputStream.  This convenience constructor creates the
  78.      * necessary intermediate OutputStreamWriter, which will convert characters
  79.      * into bytes using the default character encoding.
  80.      *
  81.      * @param  out        An output stream
  82.      *
  83.      * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
  84.      */
  85.     public PrintWriter(OutputStream out) {
  86.     this(out, false);
  87.     }
  88.  
  89.     /**
  90.      * Create a new PrintWriter from an existing OutputStream.  This
  91.      * convenience constructor creates the necessary intermediate
  92.      * OutputStreamWriter, which will convert characters into bytes using the
  93.      * default character encoding.
  94.      *
  95.      * @param  out        An output stream
  96.      * @param  autoFlush  A boolean; if true, the println() methods will flush
  97.      *                    the output buffer
  98.      *
  99.      * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
  100.      */
  101.     public PrintWriter(OutputStream out, boolean autoFlush) {
  102.     this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
  103.     }
  104.  
  105.     /** Check to make sure that the stream has not been closed */
  106.     private void ensureOpen() throws IOException {
  107.     if (out == null)
  108.         throw new IOException("Stream closed");
  109.     }
  110.  
  111.     /** Flush the stream. */
  112.     public void flush() {
  113.     try {
  114.         synchronized (lock) {
  115.         ensureOpen();
  116.         out.flush();
  117.         }
  118.     }
  119.     catch (IOException x) {
  120.         trouble = true;
  121.     }
  122.     }
  123.  
  124.     /** Close the stream. */
  125.     public void close() {
  126.     try {
  127.         synchronized (lock) {
  128.         if (out == null)
  129.             return;
  130.         out.close();
  131.         out = null;
  132.         }
  133.     }
  134.     catch (IOException x) {
  135.         trouble = true;
  136.     }
  137.     }
  138.  
  139.     /**
  140.      * Flush the stream and check its error state.  Errors are cumulative;
  141.      * once the stream encounters an error, this routine will return true on
  142.      * all successive calls.
  143.      *
  144.      * @return True if the print stream has encountered an error, either on the
  145.      * underlying output stream or during a format conversion.
  146.      */
  147.     public boolean checkError() {
  148.     if (out != null)
  149.         flush();
  150.     return trouble;
  151.     }
  152.  
  153.     /** Indicate that an error has occurred. */
  154.     protected void setError() {
  155.     trouble = true;
  156.     }
  157.  
  158.  
  159.     /*
  160.      * Exception-catching, synchronized output operations,
  161.      * which also implement the write() methods of Writer
  162.      */
  163.  
  164.     /** Write a single character. */
  165.     public void write(int c) {
  166.     try {
  167.         synchronized (lock) {
  168.         ensureOpen();
  169.         out.write(c);
  170.         }
  171.     }
  172.     catch (InterruptedIOException x) {
  173.         Thread.currentThread().interrupt();
  174.     }
  175.     catch (IOException x) {
  176.         trouble = true;
  177.     }
  178.     }
  179.  
  180.     /** Write a portion of an array of characters. */
  181.     public void write(char buf[], int off, int len) {
  182.     try {
  183.         synchronized (lock) {
  184.         ensureOpen();
  185.         out.write(buf, off, len);
  186.         }
  187.     }
  188.     catch (InterruptedIOException x) {
  189.         Thread.currentThread().interrupt();
  190.     }
  191.     catch (IOException x) {
  192.         trouble = true;
  193.     }
  194.     }
  195.  
  196.     /**
  197.      * Write an array of characters.  This method cannot be inherited from the
  198.      * Writer class because it must suppress I/O exceptions.
  199.      */
  200.     public void write(char buf[]) {
  201.     write(buf, 0, buf.length);
  202.     }
  203.  
  204.     /** Write a portion of a string. */
  205.     public void write(String s, int off, int len) {
  206.     try {
  207.         synchronized (lock) {
  208.         ensureOpen();
  209.         out.write(s, off, len);
  210.         }
  211.     }
  212.     catch (InterruptedIOException x) {
  213.         Thread.currentThread().interrupt();
  214.     }
  215.     catch (IOException x) {
  216.         trouble = true;
  217.     }
  218.     }
  219.  
  220.     /**
  221.      * Write a string.  This method cannot be inherited from the Writer class
  222.      * because it must suppress I/O exceptions.
  223.      */
  224.     public void write(String s) {
  225.     write(s, 0, s.length());
  226.     }
  227.  
  228.     private void newLine() {
  229.     try {
  230.         synchronized (lock) {
  231.         ensureOpen();
  232.         out.write(lineSeparator);
  233.         if (autoFlush)
  234.             out.flush();
  235.         }
  236.     }
  237.     catch (InterruptedIOException x) {
  238.         Thread.currentThread().interrupt();
  239.     }
  240.     catch (IOException x) {
  241.         trouble = true;
  242.     }
  243.     }
  244.  
  245.  
  246.     /* Methods that do not terminate lines */
  247.  
  248.     /** Print a boolean. */
  249.     public void print(boolean b) {
  250.     write(b ? "true" : "false");
  251.     }
  252.  
  253.     /** Print a character. */
  254.     public void print(char c) {
  255.     write(String.valueOf(c));
  256.     }
  257.  
  258.     /** Print an integer. */
  259.     public void print(int i) {
  260.     write(String.valueOf(i));
  261.     }
  262.  
  263.     /** Print a long. */
  264.     public void print(long l) {
  265.     write(String.valueOf(l));
  266.     }
  267.  
  268.     /** Print a float. */
  269.     public void print(float f) {
  270.     write(String.valueOf(f));
  271.     }
  272.  
  273.     /** Print a double. */
  274.     public void print(double d) {
  275.     write(String.valueOf(d));
  276.     }
  277.  
  278.     /** Print an array of chracters. */
  279.     public void print(char s[]) {
  280.     write(s);
  281.     }
  282.  
  283.     /** Print a String. */
  284.     public void print(String s) {
  285.     if (s == null) {
  286.         s = "null";
  287.     }
  288.     write(s);
  289.     }
  290.  
  291.     /** Print an object. */
  292.     public void print(Object obj) {
  293.     write(String.valueOf(obj));
  294.     }
  295.  
  296.  
  297.     /* Methods that do terminate lines */
  298.  
  299.     /** Finish the line. */
  300.     public void println() {
  301.     synchronized (lock) {
  302.         newLine();
  303.     }
  304.     }
  305.  
  306.     /** Print a boolean, and then finish the line. */
  307.     public void println(boolean x) {
  308.     synchronized (lock) {
  309.         print(x);
  310.         newLine();
  311.     }
  312.     }
  313.  
  314.     /** Print a character, and then finish the line. */
  315.     public void println(char x) {
  316.     synchronized (lock) {
  317.         print(x);
  318.         newLine();
  319.     }
  320.     }
  321.  
  322.     /** Print an integer, and then finish the line. */
  323.     public void println(int x) {
  324.     synchronized (lock) {
  325.         print(x);
  326.         newLine();
  327.     }
  328.     }
  329.  
  330.     /** Print a long, and then finish the line. */
  331.     public void println(long x) {
  332.     synchronized (lock) {
  333.         print(x);
  334.         newLine();
  335.     }
  336.     }
  337.  
  338.     /** Print a float, and then finish the line. */
  339.     public void println(float x) {
  340.     synchronized (lock) {
  341.         print(x);
  342.         newLine();
  343.     }
  344.     }
  345.  
  346.     /** Print a double, and then finish the line. */
  347.     public void println(double x) {
  348.     synchronized (lock) {
  349.         print(x);
  350.         newLine();
  351.     }
  352.     }
  353.  
  354.     /** Print an array of characters, and then finish the line. */
  355.     public void println(char x[]) {
  356.     synchronized (lock) {
  357.         print(x);
  358.         newLine();
  359.     }
  360.     }
  361.  
  362.     /** Print a String, and then finish the line. */
  363.     public void println(String x) {
  364.     synchronized (lock) {
  365.         print(x);
  366.         newLine();
  367.     }
  368.     }
  369.  
  370.     /** Print an Object, and then finish the line. */
  371.     public void println(Object x) {
  372.     synchronized (lock) {
  373.         print(x);
  374.         newLine();
  375.     }
  376.     }
  377.  
  378. }
  379.